#ifndef SL_ASYNFRAME_H_20220117_
#define SL_ASYNFRAME_H_20220117_

//实现多线程，会话机制，事件机制

#include <stdint.h>
#include <string.h>
#include "SLT_memory.h"
#include "SLT_smartEmbedPtr.h"
#include <memory>


//通过异步框架发送的数据。如果对端不存在则释放只能指针
class SLF_sendMemBase  : public SLT_EmbedSptrCounterBase<SLF_sendMemBase>
{
public:
	~SLF_sendMemBase(){
	}
	virtual char* getData() { return m_mem.get(); }
	virtual uint32_t   getLen() { return m_mem.getLen(); }
	virtual void  add(const char *_pdata,uint32_t _len) {
		m_mem.add(_pdata, _len);
	}
	virtual void resize(uint32_t _len) {
		m_mem.resize(_len);
	}
	SLT_memBuf   m_mem;
};
typedef SLT_EmbedSmartPtr< SLF_sendMemBase> SLF_sendMemBasePtr;

//记录事件的源和目的信息
class SLF_sender
{
public:
	virtual ~SLF_sender() {}
	virtual bool SLF_postMsg(uint32_t _SessID, int msgid, uint32_t _srcID,const SLF_sendMemBasePtr &_data) = 0;
};
typedef std::shared_ptr< SLF_sender> SLF_senderPtr;

class SLF_SessionTaskBase : public SLT_EmbedSptrCounterBase<SLF_SessionTaskBase>
{
public:
	virtual ~SLF_SessionTaskBase() {}

	virtual void setTimer(uint32_t timerID, int _intervalMs, bool periodic) = 0;
	virtual void delSelf() = 0;
	virtual uint32_t getSessID() = 0;
};
typedef SLT_EmbedSmartPtr< SLF_SessionTaskBase>  SLF_SessionTaskBasePtr;

#define  SLF_MSGARRIVE(_msgid,_fun) case _msgid:return _fun(_sender,srcSid,msgid,_data);

//会话对象。线程调度对象
class SLF_SessionBase
{
public:
	virtual ~SLF_SessionBase() {}
	virtual void OnStart() = 0;
	virtual void OnStop() = 0;
	virtual void OnMsgArrive(SLF_senderPtr &_sender,uint32_t srcSid, int msgid,SLF_sendMemBasePtr &_data) = 0;
	virtual void OnTimer(uint32_t timerID, int _intervalMs, bool periodic) = 0;

	virtual uint32_t getSessID() {return m_sessID;}
	void     setTimer(uint32_t timerID, int _intervalMs, bool periodic)
	{
		if (m_taskPtr) {
			m_taskPtr->setTimer(timerID, _intervalMs, periodic);
		}
	}
public:
	//内部关系接口
	virtual void	 setTaskPtr(const SLF_SessionTaskBasePtr& _ptr) {
		m_taskPtr = _ptr;
		if (m_taskPtr) {
			m_sessID = m_taskPtr->getSessID();
		}
		else {
			m_sessID = 0;
		}
	}
	virtual SLF_SessionTaskBasePtr &getTaskPtr(){return m_taskPtr;}
private:
	SLF_SessionTaskBasePtr m_taskPtr; //内部管理对象 SLF_SessionTask, 赋值后不能删除
	uint32_t               m_sessID = 0;
};
typedef std::shared_ptr< SLF_SessionBase>  SLF_SessionBasePtr;

//sessionid 全局通用。主要是为了区分使用不同的线程
class SLF_frame
{
public:
	virtual int SLF_init(int _threadNum) = 0;
	//uint32_t SLF_uninit(int _threadNum);

	virtual int SLF_start() = 0;
	//uint32_t SLF_stop();

	/*
	* 添加任务对象
	*/
	virtual int SLF_addSession(const SLF_SessionBasePtr&_psession) = 0;
	virtual void SLF_delSession(uint32_t _sessId) = 0;
	virtual bool SLF_postMsg(uint32_t _SessID, int msgid, uint32_t _srcID,const SLF_sendMemBasePtr& _data) = 0;
};

//创建框架对象
SLF_frame* SLF_CreateFrame();
//void      SLF_DestoryFrame(SLF_frame *pf);



#endif